package com.ruoyi.business.common.riskRule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.AppDeviceInfo;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.service.*;
import com.ruoyi.business.Vo.RulePremiseVo;
import com.ruoyi.business.component.MqProduce;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor
public class LoanRule implements RuleStrategy {

    private IAppDeviceInfoService appDeviceInfoService;

    private IApplyContactsService iApplyContactsService;

    private IDeviceAppListService iDeviceAppListService;

    private IDeviceContactsService iDeviceContactsService;

    private ICustomerInfoService customerInfoService;

    private IAppDeviceInfoService iAppDeviceInfoService;

    private MqProduce mqProduce;


    @Override
    public String getType() {
        return LOAN;
    }

    @Override
    public void rulePremise(RulePremiseService rulePremiseService, RulePremiseVo rulePremiseVo, String... columnK) {

        //设备指纹是否一致  授信是否在7天内
        int applyExpire = applyInfoExpire(rulePremiseVo.getFingerprint(), rulePremiseVo.getCustNo(), rulePremiseVo.getSerialNo());
        if (applyExpire != 0 && !rulePremiseService.operationFlag(rulePremiseVo, columnK)) {
            return;
        }
        //授信7天内新增关联
        if (applyExpire == 0) {
            rulePremiseService.saveLoanRelationFlag(rulePremiseVo.getSerialNo(), rulePremiseVo.getCustNo());
        }
        //进入队列
        mqProduce.sendLoanApplyOrderly(rulePremiseVo.getSerialNo(), rulePremiseVo.getCustNo());
//        LoanServiceImpl loanService = SpringUtils.getBean(LoanServiceImpl.class);
//        loanService.rulePass(rulePremiseVo.getSerialNo());
    }

    @Override
    public void rulePremiseV228(RulePremiseService rulePremiseService, RulePremiseVo rulePremiseVo) {

        if (!rulePremiseService.operationFlagV228(rulePremiseVo)) {
            return;
        }
        //进入队列
        mqProduce.sendLoanApplyOrderly(rulePremiseVo.getSerialNo(), rulePremiseVo.getCustNo());
//        LoanServiceImpl loanService = SpringUtils.getBean(LoanServiceImpl.class);
//        loanService.rulePass(rulePremiseVo.getSerialNo());
    }

    @Override
    public void passToRisk(RulePremiseVo rulePremiseVo) {

    }

    /**
     * 校验授权信息是否在7天有效期并且设备指纹一致
     *
     * @return -1 全部需上传; 0 无需上传; 1 上传appList ;2 上传通讯录
     */
    @Override
    public int applyInfoExpire(String fingerprint, String custNo, String excludeSerialNo) {

        //设备指纹不一致
        if (!this.fingerprintsSame(fingerprint, excludeSerialNo, custNo)) {
            // 全部上传
            return -1;
        }

        CustomerInfo customerInfo = customerInfoService.getOne(new QueryWrapper<CustomerInfo>().eq("cust_no", custNo).select("user_id"));
        fingerprint = iAppDeviceInfoService.getDeviceIdByUserId(customerInfo.getUserId(), DateUtils.getNowDate());
        // 7天内是否已上传AppList
        boolean daysAppList = Func.isNotEmpty(iDeviceAppListService.get7DaysAppList(fingerprint));
        //7天内是否已上传通讯录
        String tableName = iApplyContactsService.getTableName(customerInfo.getUserId());
        boolean daysContactsCount = false;
        if (StringUtils.isNotBlank(tableName)) {
            daysContactsCount = Func.isNotEmpty(iApplyContactsService.get7DaysContactsCount(tableName, customerInfo.getUserId()));
        } else {
            String deviceTableName = iDeviceContactsService.getTableName(fingerprint);
            daysContactsCount = Func.isNotEmpty(iDeviceContactsService.get7DaysContactsCount(deviceTableName, fingerprint));
        }
        // 无需上传
        if (daysAppList && daysContactsCount) {
            return 0;
        }
        // 全部上传
        if (!daysAppList && !daysContactsCount) {
            return -1;
        }
        // 上传AppList  上传通讯录 全部需上传
        return !daysAppList ? 1 : !daysContactsCount ? 2 : -1;
    }

    /**
     * 设备指纹是否一致
     *
     * @param currentFingerprint
     * @param excludeSerialNo
     * @param custNo
     * @return true 一致. false 不一致
     */
    @Override
    public boolean fingerprintsSame(Object currentFingerprint, String excludeSerialNo, String custNo) {
        if (Func.isEmpty(currentFingerprint)) {
            return false;
        }
        QueryWrapper<AppDeviceInfo> queryWrapper = new QueryWrapper<AppDeviceInfo>().eq("cust_no", custNo).in("action_type", "loan", "apply").last("order by id desc limit 1");
        if (Func.isNotEmpty(excludeSerialNo)) {
            queryWrapper.ne("action_key", excludeSerialNo);
        }
        AppDeviceInfo appDeviceInfo = appDeviceInfoService.getOne(queryWrapper);
        return Func.isNotEmpty(appDeviceInfo) && currentFingerprint.toString().equals(appDeviceInfo.getFingerprint());
    }


}
